<?php
App::uses('AppController', 'Controller');
/**
 * Authorizations Controller
 *
 * @property Authorization $Authorization
 */
class AuthorizationsController extends AppController {


/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Authorization->recursive = 1;
		$this->set('authorizations', $this->paginate());
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Authorization->id = $id;
		if (!$this->Authorization->exists()) {
			throw new NotFoundException(__('Invalid authorization'));
		}
		
		if(isset($this->passedArgs['p']) && !empty($this->passedArgs['p'])){
			// dodajemy do domeny
			$this->Authorization->Project->id = $this->passedArgs['p'];
			if ($this->Authorization->Project->exists()) {
				$this->request->data['Project']['project_id'] = $this->passedArgs['p'];
			}
		}
		
		if(isset($this->passedArgs['d'])){
			// dodajemy do domeny
			$this->Authorization->Domain->id = $this->passedArgs['d'];
			if ($this->Authorization->Domain->exists()) {
				$data['Domain']['domain_id'] = $this->passedArgs['d'];
				$this->request->data = $data;
			}
		}
		if(isset($this->passedArgs['p'])){
			// dodajemy do domeny
			$this->Authorization->Project->id = $this->passedArgs['p'];
			if ($this->Authorization->Project->exists()) {
				$data['Project']['project_id'] = $this->passedArgs['p'];
				$this->request->data = $data;
			}
		}
				
		$this->set('authorization', $this->Authorization->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Authorization->create();
			if ($this->Authorization->save($this->request->data)) {
				$this->Session->setFlash(__('The authorization has been saved'));
				if(isset($this->passedArgs['p']) && !empty($this->passedArgs['p'])) $this->redirect(array('controller'=>'projects','action' =>'view',$this->passedArgs['p']));
				if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])) $this->redirect(array('controller'=>'domains','action' =>'view',$this->passedArgs['d'],'d'=>'authorization'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The authorization could not be saved. Please, try again.'));
			}
		}
		
		if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d']) || isset($this->passedArgs['p']) && !empty($this->passedArgs['p'])){
			if(isset($this->passedArgs['d'])){
				// dodajemy do domeny
				$this->Authorization->Domain->id = $this->passedArgs['d'];
				if ($this->Authorization->Domain->exists()) {
					$data['Domain']['Domain']['domain_id'] = $this->passedArgs['d'];
					$this->request->data = $data;
				}
			}
			if(isset($this->passedArgs['p'])){
				// dodajemy do domeny
				$this->Authorization->Project->id = $this->passedArgs['p'];
				if ($this->Authorization->Project->exists()) {
					$data['Project']['Project']['project_id'] = $this->passedArgs['p'];	
					$this->request->data = $data;
				}
			}
			
			$domains = $this->Authorization->Domain->find('list');
			$projects = $this->Authorization->Project->find('list');
			$this->set(compact('domains', 'projects'));
		}else{
			$this->Session->setFlash(__('The authorization value could not be saved. Please, select first domain or project'));
			$this->redirect($this->referer());
		}
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Authorization->id = $id;
		if (!$this->Authorization->exists()) {
			throw new NotFoundException(__('Invalid authorization'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Authorization->save($this->request->data)) {
				$this->Session->setFlash(__('The authorization has been saved'));
				if(isset($this->passedArgs['p']) && !empty($this->passedArgs['p'])) $this->redirect(array('controller'=>'projects','action' =>'view',$this->passedArgs['p']));
				if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])) $this->redirect(array('controller'=>'domains','action' =>'view',$this->passedArgs['d']));
				
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The authorization could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Authorization->read(null, $id);
		}
		
		if(isset($this->passedArgs['p'])){
			// dodajemy do domeny
			$this->Authorization->Project->id = $this->passedArgs['p'];
			if ($this->Authorization->Project->exists()) {
				$this->request->data['Project']['project_id'] = $this->passedArgs['p'];
			}
		}
		
		$domains = $this->Authorization->Domain->find('list');
		$projects = $this->Authorization->Project->find('list');
		$this->set(compact('domains', 'projects'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Authorization->id = $id;
		if (!$this->Authorization->exists()) {
			throw new NotFoundException(__('Invalid authorization'));
		}
		if ($this->Authorization->delete()) {
			$this->Session->setFlash(__('Authorization deleted'));
			if(isset($this->passedArgs['p']) && !empty($this->passedArgs['p'])) $this->redirect(array('controller'=>'projects','action' =>'view',$this->passedArgs['p']));
			if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])) $this->redirect(array('controller'=>'domains','action' =>'view',$this->passedArgs['d'],'d'=>'authorization'));
			
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Authorization was not deleted'));
		if(isset($this->passedArgs['p']) && !empty($this->passedArgs['p'])) $this->redirect(array('controller'=>'projects','action' =>'view',$this->passedArgs['p']));
		if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])) $this->redirect(array('controller'=>'domains','action' =>'view',$this->passedArgs['d'],'d'=>'authorization'));
		
		$this->redirect(array('action' => 'index'));
	}
}
